package org.tabaddict.widget;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TAButton extends Activity implements OnClickListener {
	static final long LEARNING_DELAY =48*60*60*1000;
	static final long MIN_DELAY = 30*1000;
	static final long HL_DELAY = 6*60*60*1000;
	TADbAdapter db = null;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Button bok;
		Button bnok;
		super.onCreate(savedInstanceState);
		//if (true) {
		//setContentView(R.layout.ta_learning);
		//return;
		//}
		if (db == null) {
			db = new TADbAdapter(this);
			db.open();
		}
		long ct = System.currentTimeMillis();
		long lcig = db.getNewerDateRecord();
		long fcig = db.getOlderDateRecord();
		if (lcig == 0) {
			db.createNewRecord(1000);
			lcig = db.getNewerDateRecord();
			fcig = db.getOlderDateRecord();
		}
		if ((ct - lcig) < MIN_DELAY) {
			setContentView(R.layout.ta_cancel);
			bok = (Button)findViewById(R.id.ta_cancel_ok);
			bok.setOnClickListener(this);
			bnok = (Button)findViewById(R.id.ta_cancel_cancel);
			bnok.setOnClickListener(this);
		} else if ((ct - fcig) < LEARNING_DELAY) {
			setContentView(R.layout.ta_learning);
			bok = (Button)findViewById(R.id.ta_learning_ok);
			bok.setOnClickListener(this);
			bnok = (Button)findViewById(R.id.ta_learning_cancel);
			bnok.setOnClickListener(this);
		} else {
			if(okOrNOk(ct) == true) {
				setContentView(R.layout.ta_ok);
				bok = (Button)findViewById(R.id.ta_ok_ok);
				bok.setOnClickListener(this);
				bnok = (Button)findViewById(R.id.ta_ok_cancel);
				bnok.setOnClickListener(this);
			} else {
				setContentView(R.layout.ta_nok);
				bok = (Button)findViewById(R.id.ta_nok_force);
				bok.setOnClickListener(this);
				bnok = (Button)findViewById(R.id.ta_nok_cancel);
				bnok.setOnClickListener(this);
			}
		}
	}

	private boolean okOrNOk(long ct) {
		TARecord cig[] = db.getRecords(ct - LEARNING_DELAY);
		double nico = 0;
		double nicomx = 0;
		if (cig == null) return true;
		for(int i = 0; i<cig.length; i++) {
			double t = ((double)(ct - cig[i].date))/(double)HL_DELAY;
			t = cig[i].percent/Math.pow(2., t);
			nico += t;
		}
		TANico[] onicos = db.getNicos(5);
		if (onicos == null || onicos[0].date < ct - LEARNING_DELAY / 2) {
			nicomx = computeNicoMx(cig);
			db.setNico(nicomx);
		} else {
			for (int i = 0; i<onicos.length; i++)
				nicomx += onicos[i].nico;
			nicomx = nicomx/onicos.length;
		}
		return (nico + 10. < nicomx);
	}

	private double computeNicoMx(TARecord[] cig) {
		double nicomx = 0;
		for(int i = 0; i<cig.length; i++) {
			double nico = 0;
			for(int j = 0; j<i; j++){
				double t = ((double)(cig[i].date - cig[j].date))/(double)HL_DELAY;
				t = cig[j].percent/Math.pow(2., t);
				nico += t;
			}
			for(int j = i; j<cig.length; j++) {
				double t = ((double)(cig[i].date - cig[j].date + LEARNING_DELAY))/(double)HL_DELAY;
				t = cig[j].percent/(Math.pow(2., t));
				nico += t;
			}
			nicomx = Math.max(nicomx, nico);
		}		
		return nicomx;
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.ta_nok_force || id == R.id.ta_ok_ok || id == R.id.ta_learning_ok) {
			db.createNewRecord(100);
		} else if (id == R.id.ta_cancel_ok) {
			db.setLastRecordPercent(0);
		}
		finish();
	}

	@Override
	protected void onResume() {
		if (db == null) {
			db = new TADbAdapter(this);
			db.open();			
		}
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		if (db != null) {
			db.close();
			db = null;
		}
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		if (db != null) {
			db.close();
			db = null;
		}
		super.onDestroy();
	}

}